package com.witmed.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.witmed.entity.Article;
import com.witmed.entity.Doctor;
import com.witmed.service.ArticleService;
import com.witmed.service.impl.ArticleServiceImpl;
import com.witmed.util.PageInfo;

/**
 * Servlet implementation class ArticleServlet
 */
@WebServlet("/ArticleServlet")
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ArticleService articleService =new ArticleServiceImpl();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String op = request.getParameter("op");//获取操作符
		 switch (op) {
		case "seachArticle":
			doseachArticle(request,response);
			break;
		case "searchByPage":
			dofindByPage(request,response);
			break;
		case "insertContent":
			doInsertContent(request,response);
			break;
		case "seachAllArticle":
			doseachAllArticle(request,response);
			break;
		case "updateArticleContent":
			doUpdateArticleContent(request,response);
			break;
		case "articleId":
			dodeleteArticleId(request,response);
		default:
			break;
		}
	}

	private void dodeleteArticleId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String articleId = request.getParameter("articleId");
		boolean b = articleService.deleteById(Integer.parseInt(articleId));
		if (b) {
			ObjectMapper objectMapper =new ObjectMapper();
			String json = objectMapper.writeValueAsString(b);
			//将数据响应回前端
			PrintWriter out = response.getWriter();
			out.print(json);
		}
	}

	private void doUpdateArticleContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String updateArticleContent = request.getParameter("articleContent");
		String articleTitle = request.getParameter("articleTitle");
		Integer articleId = Integer.parseInt(request.getParameter("articleId"));
		boolean b = articleService.update(new Article(articleId, articleTitle, updateArticleContent, null, null));
		System.out.println(b);
		if (b) {
			ObjectMapper objectMapper =new ObjectMapper();
			String json = objectMapper.writeValueAsString(b);
			//将数据响应回前端
			PrintWriter out = response.getWriter();
			out.print(json);
		}
	}

	private void doseachAllArticle(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Article> articleList = articleService.getFindByAll();
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(articleList);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter  out= response.getWriter();
		out.print(json);
		
	}

	private void doInsertContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String publishArticlecontent = request.getParameter("publishArticlecontent");
		String publishArticleTime =request.getParameter("publishArticleTime");
		String publishArticletitle = request.getParameter("publishArticletitle");
		Integer publishArticletitleDoctor = Integer.parseInt(request.getParameter("publishArticletitleDoctor"));
		boolean b = articleService.insert(new Article(null, publishArticletitle, publishArticlecontent, publishArticleTime, new Doctor(publishArticletitleDoctor, null, null, null, null, null, null, null)));
		if (b) {
			ObjectMapper objectMapper =new ObjectMapper();
			String json = objectMapper.writeValueAsString(b);
			//将数据响应回前端
			PrintWriter out = response.getWriter();
			out.print(json);
		}
	}

	private void dofindByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String article = request.getParameter("SearchWords");
		PageInfo<Article> pageInfo = articleService.getFindByPage(pageNum, pageSize, new Article(null, article, null, null, new Doctor(null, null, null, null, null, null, null, null)));
		// 封装数据
	    ObjectMapper objectMapper = new ObjectMapper();
	    String json = objectMapper.writeValueAsString(pageInfo);
	    // 设置响应内容类型
	    response.setContentType("application/json;charset=utf-8");
	    // 响应数据
	    PrintWriter  out= response.getWriter();
	    out.print(json);
	}
		

	private void doseachArticle(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String articleTitle = request.getParameter("articleTitle");
		List<Article> articleList = articleService.getFindByArticleTitle(articleTitle);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(articleList);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter  out= response.getWriter();
		out.print(json);
	}

}
